package com.bigfire.dtm.starter.datasource;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Optional;

/**
 * 汉薇Axon框架专用数据源
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月05日 11:35
 */
//@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "hwAxonEntityManagerFactory",
        transactionManagerRef = "hwAxonTransactionManager")
@RequiredArgsConstructor
public class HwAxonDataConfig {

    @Qualifier("hwAxonDataSource")
    private final DataSource hwAxonDataSource;
    private final HibernateProperties hibernateProperties;
    private final JpaProperties jpaProperties;
    private final ApplicationContext context;


    @Bean(name = "hwAxonEntityManager")
    public EntityManager hwAxonEntityManager(EntityManagerFactoryBuilder builder) {
        EntityManagerFactory factory =
                Optional.ofNullable(hwAxonEntityManagerFactory(builder))
                        .orElseThrow(NullPointerException::new)
                        .getObject();
        return Optional.ofNullable(factory).orElseThrow(NullPointerException::new).createEntityManager();
    }


    @Bean(name = "hwAxonEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean hwAxonEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(hwAxonDataSource)
                .packages("org.axonframework.eventsourcing.eventstore.jpa",
                        "org.axonframework.modelling.saga.repository.jpa",
                        "org.axonframework.eventhandling.tokenstore.jpa")
                .persistenceUnit("hwAxonPersistenceUnit")
                .properties(hibernateProperties.determineHibernateProperties(
                        jpaProperties.getProperties(), new HibernateSettings()))
                .build();
    }


    @Bean(name = "hwAxonTransactionManager")
    public PlatformTransactionManager hwAxonTransactionManager(EntityManagerFactoryBuilder builder) {
        EntityManagerFactory factory =
                Optional.ofNullable(hwAxonEntityManagerFactory(builder))
                        .orElseThrow(NullPointerException::new)
                        .getObject();
        return new JpaTransactionManager(
                Optional.ofNullable(factory).orElseThrow(NullPointerException::new));

    }
}